import { Modal } from "ant-design-vue";
import { useRouter } from 'vue-router';
import { deleteByIds } from "#/api";

const router = useRouter();
const [modal] = Modal.useModal();
modal.centered = true;

// 编辑操作的 handler
export const handleUpdate = (rowData) => {
  router.push({
    path: '/form',
    query: {
      type: "UPDATE",
      className: rowData.idKey,
      pid: rowData.pid,
    },
  });
};

// 删除操作的 handler
export const handleDelete = (rowData, actionType, rowDataSet, emit) => {
  modal.confirm({
    title: actionType === 'top' && rowDataSet.size > 1 ? "确定要删除这些数据？" : "确定要删除这条数据？",
    centered: true,
    content: '删除操作不可恢复，请谨慎操作！',
    onOk() {
      let params = actionType === 'top' ? Array.from(rowDataSet) : [rowData.pid];
      deleteByIds(params).then(() => {
        emit('doAction');
      }).catch(error => {
        console.error("删除失败：", error);
      });
    },
    onCancel() { },
  });
};
